$(document).ready(function () {
    questionList();
});

var count;
function questionList() {
    $.ajax({
        type: "POST",
        url: "zgtAnswerLists",
        data: {},
        dataType: "json",
        success: function (data) {
            count = data.length;
            var str = "";
            for (var i = 0; i < data.length; i++) {
                str += i+1 + "、" +data[i].question.questionName + "<br><br>";
                str += "<textarea style='width:420px; height:100px;' readonly>" + data[i].questionA + "</textarea>" +
                    "本题判分:<input id='tip1" + i + "' type='text'><hr>";
                str += "<input id='tip2" + i + "' type='hidden' value='"+ data[i].id + "'>"
                str += "<input id='tip3" + i + "' type='hidden' value='"+ data[i].questionId + "'>"
                str += "<input id='tip4" + i + "' type='hidden' value='"+ data[i].createTime + "'>"
                str += "<input id='tip5" + i + "' type='hidden' value='"+ data[i].gradeId + "'>"
            }
            $("#tablist").html(str);
            if(str == ""){
                $("#mark").hide();
            }
            if(str != ""){
                $("#mark").show();
            }
        }
    });
}

var gradeData = [];
function givemark() {
    gradeData.length = 0;
    for (var k = 0; k < count; k++) {
        var tip6 = "tip1" + k;
        var tip7 = $("#" + tip6).val();   //本题分数
        var tip8 = "tip2" + k;
        var tip9 = $("#" + tip8).val();  //teacherGrade的ID
        var tip10 = "tip3" + k;
        var tip11 = $("#" + tip10).val();  //试题的ID
        var tip12 = "tip4" + k;
        var tip13 = $("#" + tip12).val();  //创建时间
        var tip14 = "tip5" + k;
        var tip15 = $("#" + tip14).val();  //gradeId
        gradeData.push(tip7 + "&" + tip9 + "&" + tip11 + "&" + tip13 + "&" + tip15);
    }
    if(isnull()){
        $.ajax({
            type:"POST",
            url:"giveMark",
            data:{"gradeData":gradeData},
            dataType:"json",
            success:function(data){
                if(data == 1){
                    layer.alert("提交成功");
                    window.location.reload(true);
                }
                else {
                    layer.alert("提交失败，请与管理员联系");
                }
            },
            error:function(){
                layer.alert("提交失败，请与管理员联系");
            }
        });
    }
}

//输入框文本域判空
function isnull() {
    var inputObj = document.getElementsByTagName("input");
    for (var i = 0; i < inputObj.length; i++) {
        if (inputObj[i].type == "" || inputObj[i].type == "text") {
            if (inputObj[i].value == "" || inputObj[i].value == null) {
                inputObj[i].focus();
                inputObj[i].style.background = "red";
                layer.alert("有试题没有判分，不能提交");
                inputObj[i].style.background = "red";
                return false;
                break;
            }
            if(!(/^(\d|10){1}$/).test(inputObj[i].value)){
                inputObj[i].focus();
                inputObj[i].style.background = "red";
                layer.alert("分数只能在0-10分之间，且为整数");
                inputObj[i].style.background = "red";
                return false;
                break;
            }
        }
    }
    //	alert("成功：数据都不为空！");
    return true;
}